package com.huaonline.music.player.commonutils;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;

public class FileCommonUtil {
	private static Context ctx;
	private static final String NO_MP3_FILE = "NO MP3 FILES";
	public static final String AUDIO_PATH = "AUDIO_PATH";
	public static final String AUDIO_NAME = "AUDIO_NAME";

	FileCommonUtil(Context context) {
		this.ctx = context;
	}
	public static String getFileName(String filePath){
		return new File(filePath).getName();
	}
	public static ArrayList<Map<String, String>> findMP3InSD(Context ctx) {
		ArrayList<Map<String, String>> data = new ArrayList<Map<String, String>>();
		if (ctx == null) {
			ToastCommonUtils.getToastShort(NO_MP3_FILE, ctx);
		} else {
			ContentResolver cr = ctx.getContentResolver();
			String[] columns = new String[] { MediaStore.Audio.Media.TITLE,
					MediaStore.Audio.Media.DATA };
			Uri audio_uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
			String selection = MediaStore.Audio.Media.IS_MUSIC + "=?";

			Cursor cursor = cr.query(audio_uri, columns, selection,
					new String[] { String.valueOf(1) }, null);
			cursor.moveToFirst();
			while (cursor.moveToNext()) {
				System.out.println(cursor.getString(1));
				System.out.println(cursor.getString(0));
				String filePath = cursor.getString(1);
				Map<String, String> map = new HashMap<String, String>();
				map.put(AUDIO_PATH, filePath);
				map.put("AUDIO_NAME", cursor.getString(0));
				data.add(map);
			}
			cursor.close();

		}
		return data;
	}
}
